<?php
global $smarty;

$thisPage = "invoices";

$params = unserialize(stripslashes(getRequestParameter("params")));

addDataservice("getinvoices");
addDataService("invoicepayed");

function buildLayout($smarty) {
    global $thisPage;
    global $params;
    global $titles;

    TemplateHelper::useLayout("default", true);
    $smarty->assign("default", array(
        content => getContent($smarty),
        contexthelp => getContextHelp($smarty),
    ));
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "getinvoices" : { return service_getinvoices($params); }
        case "invoicepayed" : { return service_invoicepayed($params); }
    }
}

function service_getinvoices($params) {
    Database::loadModel("payment");
    $result = Database::getModel()->getInvoices(getRequestParameter("pg"), getRequestParameter("rows"));
    $reccount = Database::getModel()->getInvoiceCount();
    $limit = getRequestParameter("rows");
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "payment_id"),
    );
}

function service_invoicepayed($params) {
    Database::loadModel("payment");
    $invoiceData = Database::getModel()->getInvoiceData($params['id']);
    Database::getModel()->setStatus(PaymentModel::ST_PAYED, $params['id']);
//    Database::getModel()->removeInvoice($params['id']);
    Database::loadModel("companies");
    Database::getModel()->sponsorationPayed($invoiceData);
    return serviceSuccessful(array());
}

function getContent($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function getcontextHelp() {
}

function getPageHeader() {
}

function getPageFooter() {
}

?>
